/**
 * list:
 *    select all
 *    plupload
 *    zeroclipboard
 *    run js
 *    slide
 *    cascade
 *    resizable
 *    tooltip ?
 *    lazyload
 */

/**
 * drag download for chrome ( drag target to local folder download)
 * <decode class="js-drag-download" data-download-url="MIME:FileName:URL"></decode>
 */
(function ($) {
	// 拖拽下载(拖动到本地文件夹下载) Chrome 没问题, FF 下载的是 :inetloc
	$('.js-drag-download').each(function (i, el) {
		// 拖动一切能拖动的东西吧 (draggable=true)
		// if (Modernizr.draganddrop) {
		el.addEventListener && el.addEventListener("dragstart", function (e) {
			// 拼装URL: 必须是{MiME}:{FileName}:{FileUrl}
			// var url = 'image/jpeg:Penguins.jpg:http://img0.bdstatic.com/img/image/shouye/gxnr34.jpg';
			var url = (this.dataset && this.dataset["download-url"]) || this.getAttribute("data-download-url");
			// if (e.dataTransfer && e.dataTransfer.constructor == Clipboard && e.dataTransfer.setData('DownloadURL', 'http://www.box.net')) {
			e.dataTransfer.setData("DownloadURL", url);
		}, false);
		// } else { fallback }
	});
	$('.js-drag-download').css({
		textDecoration: "none",
		cursor: "default"
	});
})(jQuery);

/**
 * Run Code
 * <button class="js-run" data-run-text="xxxx">Run Code</button>
 * <button class="js-run" data-run-target="textAreaId">Run Code</button>
 */
(function ($) {
	function run(html) {
		var win = window.open('', '_blank', '');
		win.document.open('text/html', 'replace');
		win.opener = null;  // 防止对宿主页操作
		win.document.write(html);
		win.document.close();
	}

	$('button.js-run').click(function () {
		var me = $(this),
			code = me.attr('data-run-text');
		if (!code) {
			code = me.attr('data-run-target');
			code = code && $('#' + code).val();
		}
		code = code || '';
		run(code);
	});
})(jQuery);

/**
 * Zero Clipboard
 * <button class="js-zeroclipboard" data-clipboard-text="content"></button>
 * <button class="js-zeroclipboard" data-clipboard-text="content" aral-label="Copy to clipboard" data-copied-hint="Copied"></button>
 * <button class="js-zeroclipboard" data-clipboard-target="el id" aral-label="Copy to clipboard" data-copied-hint="Copied"></button>
 * <p />
 * ZeroClipboard v1.x is expected to work in IE6+ and all of the evergreen browsers.
 */
window.ZeroClipboard && (function ($, ZeroClipboard) {
	ZeroClipboard.setDefaults({
		// moviePath: "javascripts/zc/v" + ZeroClipboard.version + "/ZeroClipboard.swf"
		moviePath: 'js/lib/zeroclipboard-' + ZeroClipboard.version + '/ZeroClipboard.swf'
	});

	var el = $("button.js-zeroclipboard"),
		clip = new ZeroClipboard(el),
		bridge = $("#global-zeroclipboard-html-bridge");

	clip.on("load", function (client) {
		client.on("complete", function (client, args) {
			var hint = $(this).attr("data-copied-hint");
			$("#global-zeroclipboard-html-bridge").attr("aria-label", hint || "Copied!");
		});

		clip.on('mouseover', function (client) {
			var label = $(this).attr("aria-label"); // button aria-label attribute
			bridge.addClass("tooltipped tooltipped-s").attr("aria-label", label || "Copy to clipboard.");
		});

		clip.on('mouseout', function (client) {
			bridge.removeClass("tooltipped tooltipped-s");
		});
	});

	clip.on("noflash wrongflash", function (client) {
		return $("#global-zeroclipboard-html-bridge, .js-zeroclipboard").remove()
	});
})(jQuery, ZeroClipboard);

/*
 // ZeroClipboard v2.x is expected to work in IE9+ and all of the evergreen browsers.
 (function ($, ZeroClipboard) {
 // $.observe("button.js-zeroclipboard", function(el) {
 // ZeroClipboard.config({
 // swfPath: "" + Nothing.assetHostUrl + "flash/ZeroClipboard.v" + ZeroClipboard.version + ".swf",
 // trustedOrigins: [location.hostname],
 // flashLoadTimeout: 1e4,
 // cacheBust: null != (e = /MSIE/.test(navigator.userAgent) || /Trident\/[\d](?=[^\?]+).*rv:([0-9.].)/.test(navigator.userAgent)) ? e : { "true": false }
 // });
 var el = $("button.js-zeroclipboard"),
 clip = new ZeroClipboard(el),
 bridge = $("#global-zeroclipboard-html-bridge");

 clip.on("aftercopy", function (client) {
 var hint = $(client.target).attr("data-copied-hint");
 bridge.attr("aria-label", hint || "Copied!");
 });

 clip.on("error", function (client) {
 return $("#global-zeroclipboard-html-bridge, .js-zeroclipboard").remove()
 });

 $(el).hover(function () {
 var label = $(this).attr("aria-label");
 bridge.addClass("tooltipped tooltipped-s").attr("aria-label", label || "Copy to clipboard.")
 }, function () {
 return bridge.removeClass("tooltipped tooltipped-s")
 });
 })(jQuery, ZeroClipboard);
 */

/**
 * select all
 * &ltinput type="checkbox" class="js-select-all" data-target-name="color" &gt;
 */
(function ($) {
	$('input:checkbox:enabled.js-select-all').each(function (i, el) {
		var $el = $(el),
			targetName = $el.attr('data-target-name'),
			allTargetSelector = '[name=' + targetName + ']:enabled:checkbox',
			checkedTargetSelector = allTargetSelector + ':checked',
			$allTarget = $(allTargetSelector);

		$el.change(function () {
			$allTarget.attr('checked', !!$el.attr('checked'));
		});

		$allTarget.change(function () {
			var checked = $(this).attr('checked');

			if (!checked) {
				$el.attr('checked', false);
			} else {
				$el.attr('checked', $allTarget.length == $(checkedTargetSelector).length);
			}
		}).change();
	});
})(jQuery);

/**
 * goto top
 */
(function ($) {
	var $win = $(window),
		$el = $('.js-goto-top');

	$win.scroll(function () {
		if ($win.scrollTop() >= 200) {
			// $el.stop().css('opacity', 1).fadeIn();
            $el.css('visibility', 'visible');
		} else {
			// $el.stop().css('opacity', 0).fadeOut();
            $el.css('visibility', 'hidden');
		}
	});

	$el/*.hide()*/.click(function (e) {
		e.preventDefault();

		$('body,html').stop().animate({
			scrollTop: 0
		}, 200);

		return false;
	});
})(jQuery);
